package com.scqkxx.gamebox.activity.register;

import android.content.Intent;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;

import com.avos.avoscloud.AVCloud;
import com.avos.avoscloud.AVException;
import com.avos.avoscloud.FunctionCallback;
import com.scqkxx.gamebox.R;
import com.scqkxx.gamebox.databinding.ActivityRegisterPhoneBinding;
import com.scqkzqtz.base.base.BaseActivity;
import com.scqkzqtz.base.library.utils.ToastUtils;

import java.util.HashMap;
import java.util.Map;

public class RegisterPhoneActivity extends BaseActivity<ActivityRegisterPhoneBinding> {

    @Override
    public int setLayoutId() {
        return R.layout.activity_register_phone;
    }

    @Override
    public void init() {
        binding.mTitleView.setOnTitleClickListener((int titleType) -> finish());
        binding.useEmail.setOnClickListener((View view) -> {
            startActivity(new Intent(this,RegisterEmailActivity.class));
        });
        binding.btNext.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String username = binding.etName.getText().toString();
                if (TextUtils.isEmpty(username)) {
                    ToastUtils.showShortToast(RegisterPhoneActivity.this,"手机号不能为空");
                    return;
                }

                Map<String, Object> map = new HashMap<>();
                map.put("username", username);
                AVCloud.callFunctionInBackground("sendCodeByEmail", map, new FunctionCallback<Object>() {
                    @Override
                    public void done(Object object, AVException e) {
                        if(e == null){
                            //{msg=邮件发送成功, data={}, error_code=0}
                            Log.d("sendCodeByEmail",object.toString());
                            Map<String,Object> map = (Map<String, Object>) object;
                            String error_code = map.get("error_code")+"";
                            String msg = map.get("msg")+"";

                            ToastUtils.showShortToast(RegisterPhoneActivity.this,msg);
                            if("0".equals(error_code)){
//                                RegisterEmailActivity.this.finish();
                                startActivity(new Intent(RegisterPhoneActivity.this,RegisterCheckCodeActivity.class));
                            }
                        }else {
                            ToastUtils.showShortToast(RegisterPhoneActivity.this,e.getMessage());
                        }
                    }
                });
            }
        });
    }
}
